
import 'package:flutter/material.dart';

import 'custom_button.dart';

enum LoadType { loading, loadSuccess, loadFailure }


abstract class PageLoad {
  late LoadType loadStatus;
  late String errorMsg;
}

class LoadingView extends StatelessWidget {
  const LoadingView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Center(child: CircularProgressIndicator());
  }
}

class EmptyListView extends StatelessWidget {
  final VoidCallback? onReload;
  final String? text;

  const EmptyListView({Key? key, this.onReload, this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Image(
            image: AssetImage('images/box_null.png'),
            width: 160,
            height: 160,
          ),
          Text(
            text ?? '列表为空',
            style: const TextStyle(color: Colors.black),
          ),
          Visibility(
            visible: onReload != null,
            child: Container(
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8.0))),
                  onPressed: onReload,
                  child: Text('重新加载'),
                )),
          ),
        ],
      ),
    );
  }
}

typedef BuildBody = Widget Function(bool isLoadSuccess);

class BasePageLoadingView extends StatelessWidget {
  late LoadType loadStatus;
  late String errorMsg;
  final Function? onReload;

  final BuildBody buildBody;

  BasePageLoadingView(
      {Key? key,
      required this.buildBody,
      required this.loadStatus,
      required this.errorMsg,
      this.onReload})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return loadStatus == LoadType.loading
        ? const LoadingView()
        : loadStatus == LoadType.loadFailure
            ? LoadErrorView(
                errorMsg: errorMsg,
                onReload: onReload,
              )
            : buildBody(loadStatus == LoadType.loadSuccess);
  }
}

typedef LoadSuccess = Widget Function();
typedef LoadFailure = Widget Function();

class PageDataLoadingView extends StatelessWidget {
  final LoadType loadStatus;

  ///失败原因文本
  final String? errorMsg;

  ///失败二级提示文本
  final String? tipsMsg;
  final Color errorMsgColor;

  ///失败按钮文本
  final String? reloadText;

  ///失败按钮点击回调
  final Function()? onReload;

  ///失败按钮背景颜色
  final Color? buttonColor;

  ///自定义加载进度条
  final Widget? loadingView;

  ///自定义失败图标
  final Widget? errorIcon;

  ///成功回调
  final LoadSuccess onLoadSuccess;

  ///失败回调
  final LoadFailure? onLoadFailure;

  PageDataLoadingView({
    required this.onLoadSuccess,
    this.onLoadFailure,
    this.loadingView,
    required this.loadStatus,
    this.errorMsg,
    this.errorIcon,
    this.reloadText,
    this.tipsMsg,
    this.errorMsgColor = Colors.white,
    this.buttonColor = const Color(0xFF0099CC),
    this.onReload,
  });

  @override
  Widget build(BuildContext context) {
    if (loadStatus == LoadType.loading) {
      return loadingView ?? LoadingView();
    } else if (loadStatus == LoadType.loadFailure) {
      if (onLoadFailure != null) {
        return onLoadFailure!.call();
      } else {
        return LoadErrorView(
          errorMsg: errorMsg ?? '加载失败',
          reloadText: reloadText ?? '重新加载',
          errorIcon: errorIcon,
          tipsMsg: tipsMsg,
          errorMsgColor: errorMsgColor,
          buttonColor: buttonColor,
          onReload: onReload,
        );
      }
    } else {
      return onLoadSuccess();
    }
  }
}

class LoadErrorView extends StatelessWidget {
  final String errorMsg;
  final Function? onReload;
  final String? reloadText;
  final String? tipsMsg;
  final Color? errorMsgColor;
  final Color? buttonColor;
  final Widget? errorIcon;

  const LoadErrorView({
    super.key,
    required this.errorMsg,
    this.onReload,
    this.errorIcon,
    this.reloadText,
    this.tipsMsg,
    this.errorMsgColor = Colors.white,
    this.buttonColor = const Color(0xFF0099CC),
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        errorIcon == null
            ? const Image(
                image: AssetImage('images/box_null.png'),
                width: 160,
                height: 160,
              )
            : errorIcon!,
        Text(errorMsg, style: TextStyle(fontSize: 14, color: Colors.red)),
        SizedBox(
          width: double.infinity,
          height: 10.0,
        ),
        Text(tipsMsg ?? '刷新试试～～',
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 12, color: Colors.grey)),
        SizedBox(
          height: 20.0,
        ),
        if (onReload != null)
          CCButton(
            horizontalPadding: 30.0,
            child: Text(
              reloadText ?? '刷新',
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(fontSize: 12, color: Colors.black),
            ),
            onPressed: onReload?.call(),
          )
      ],
    );
  }
}
